package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.RequireLogin;
import cn.wolfcode.wolf2w.util.UserParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/strategies")
public class StrategyController {


    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyStatisRedisService strategyStatisRedisService;

    //查看详情
    @RequestMapping("/detail")
    public Object detail(String id) {

        Strategy strategy = strategyService.get(id);
        strategyStatisRedisService.viewnumIncrease(id, 1);
        return JsonResult.success(strategy);
    }


    //查看全部
    @RequestMapping("/query")
    public Object query(StrategyQuery qo) {
        return JsonResult.success(strategyService.query(qo));
    }


    @RequestMapping("/themes")
    public Object themes() {
        return JsonResult.success(strategyThemeService.list());
    }

    //评论列表
    @RequestMapping("/comments")
    public Object comments(StrategyCommentQuery query) {
        return JsonResult.success(strategyCommentService.query(query));
    }

    @RequestMapping("/addComment")
    @RequireLogin
    //不登录无法添加评论
    public Object addComment(StrategyComment strategyComment, @UserParam UserInfo userInfo) {
        BeanUtils.copyProperties(userInfo, strategyComment);
        strategyComment.setUserId(userInfo.getId());
        strategyCommentService.save(strategyComment);
        BeanUtils.copyProperties(userInfo, strategyComment);
        strategyComment.setUserId(userInfo.getId());

        //评论总数统计
        strategyStatisRedisService.replynumIncrease(strategyComment.getStrategyId(), 1);

        return JsonResult.success();
    }

    //点赞
    @RequestMapping("/commentThumb")
    @RequireLogin
    public Object commentThumb(String cid, String sid, @UserParam UserInfo userInfo) {

        strategyCommentService.commentThumb(cid, sid);
        return JsonResult.success();
    }

    //阅读数统计
    @GetMapping("/statisVo")
    public Object statisVo(String sid) {
        return JsonResult.success(strategyStatisRedisService.getStrategyStatisVO(sid));
    }

    //收藏数
    @PostMapping("/favor")
    @RequireLogin
    public Object favor(String sid, @UserParam UserInfo userInfo) {
        boolean ret = strategyStatisRedisService.favor(sid, userInfo.getId());
        return JsonResult.success(ret);
    }

    @PostMapping("/strategyThumbup")
    @RequireLogin
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo){

        boolean ret = strategyStatisRedisService.strategyThumbup(sid, userInfo.getId());
        return JsonResult.success(ret);
    }

}
